import { _decorator, Component, Node } from 'cc';
import { ToggleGroupController } from './ToggleGroupController';
const { ccclass, property } = _decorator;

@ccclass('ToggleController')
export class ToggleController extends Component {

    toggleKey: string;
    toggleGroup: ToggleGroupController;

    background: Node = null;
    checkmark: Node = null;

    start() {
        this.node.on(Node.EventType.TOUCH_END, this.OnClickToggle, this);
        this.OnInitPanel();
    }

    OnInitPanel() {
        this.background = this.node.getChildByName("Background");
        this.checkmark = this.node.getChildByName("Checkmark");
    }

    OnUpdateSelect(isSelected) {
        this.OnInitPanel();
        if (this.background == null || this.checkmark == null) {
            console.log("toggle name " + this.node.name);

        }
        this.background.active = !isSelected;
        this.checkmark.active = isSelected;
    }

    OnClickToggle() {
        this.OnInitPanel();
        if (this.toggleGroup) {
            this.toggleGroup.OnToggleGroup(this.toggleKey);
        }
    }

    Init(key, group) {
        this.OnInitPanel();
        this.toggleKey = key;
        this.toggleGroup = group;
    }

}

